<template>
  <div class="content">
    <el-card class="content-card">
      <div slot="header" class="row-bg-query">
        <div><el-input v-model="params.name" size="small" placeholder="请输入菜单名称" clearable /></div>
        <div><el-button type="primary" size="small" icon="el-icon-search" @click="search">搜索</el-button></div>
      </div>
      <div class="operating">
        <div><el-button type="primary" size="small" icon="el-icon-circle-plus-outline" @click="handleCreate">添加</el-button></div>
        <div><el-button type="primary" size="small" icon="el-icon-refresh-left" @click="refresh">刷新</el-button></div>
      </div>
      <el-table
        v-loading="loading"
        :data="services"
        style="width: 100%"
        row-key="id"
        border
        lazy
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column align="center" prop="id" label="ID" width="40" />
        <el-table-column align="center" prop="name" label="名称" width="140" />
        <el-table-column align="center" prop="code" label="编码" width="140" />
        <el-table-column align="center" prop="onlineStatus" label="在线状态" width="80">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.onlineStatus === 1 ? 'online' : 'offline'" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="secret" label="密钥" width="180" />
        <el-table-column align="center" prop="description" label="描述" width="140" />
        <el-table-column align="center" label="是否禁用" prop="locked" width="80">
          <template slot-scope="scope">
            <el-switch :value="scope.row.locked" :active-value="1" :inactive-value="0" @change="handleSwitch(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="loginTime" label="最近登录时间" width="180" />
        <el-table-column align="center" prop="createTime" label="创建时间" width="180" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="180" />
        <el-table-column align="center" fixed="right" label="操作" width="400">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="warning" @click="handleAllowedClient(scope.row)">查看授权客户端</el-button>
            <el-button size="mini" type="success" @click="handleAuthorityServiceInfo(scope.row)">查看允许访问的服务端</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogForm" width="30%">
      <el-form :model="form">
        <el-form-item label="名称" label-width="20%">
          <el-input v-model="form.name" class="form-input" placeholder="请输入名称..." autocomplete="off" />
        </el-form-item>
        <el-form-item label="编码" label-width="20%">
          <el-input v-model="form.code" class="form-input" placeholder="请输入路径..." autocomplete="off" />
        </el-form-item>
        <el-form-item label="是否锁定" label-width="20%">
          <el-switch v-model="form.locked" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="描述" label-width="20%">
          <el-input v-model="form.description" class="form-input" type="textarea" rows="4" placeholder="请输入描述..." autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" :loading="buttonLoading" @click="handleAddOrUpdate">保 存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="授权服务端" :visible.sync="dialogAllowedClient" width="30%">
      <el-tag v-for="(item, index) in allowedClients" :key="index" :type="index % 2 == 0 ? 'primary' : 'success'" effect="dark" style="display: flex; flex-direction: column; text-align: center; margin-bottom: 4px;">
        {{ item }}
      </el-tag>
    </el-dialog>
    <el-dialog title="允许访问的服务端" :visible.sync="dialogAuthorityServiceInfo" width="36%">
      <el-table
        :data="authorityServiceInfos"
        style="width: 100%"
        row-key="id"
        border
        lazy
      >
        <el-table-column align="center" prop="name" label="名称" width="140" />
        <el-table-column align="center" prop="code" label="编码" width="140" />
        <el-table-column align="center" prop="onlineStatus" label="在线状态" width="80">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.onlineStatus === 1 ? 'online' : 'offline'" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="description" label="描述" />
      </el-table>
    </el-dialog>
    <el-pagination
      v-if="page.pages"
      background
      layout="prev, pager, next"
      :current-page.sync="params.current"
      :page-size="page.size"
      :page-count="page.pages"
      @current-change="turnPage"
    />
  </div>
</template>

<script>
import { getList, addOrUpdate, getAllowedClient, getAuthorityServiceInfo } from '@/api/service'

export default {
  data() {
    return {
      loading: false,
      onlineStatus: {
        0: '下线',
        1: '在线'
      },
      page: {
        current: 1,
        size: 10
      },
      services: [],
      params: {
        current: 1,
        size: 10
      },
      textMap: {
        update: '修改',
        create: '添加'
      },
      dialogStatus: '',
      form: {
        id: null,
        name: null,
        locked: 0,
        code: null,
        description: null
      },
      dialogForm: false,
      buttonLoading: false,
      dialogAllowedClient: false,
      allowedClients: [],
      dialogAuthorityServiceInfo: false,
      authorityServiceInfos: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取标签列表
    fetchData() {
      this.loading = true
      getList(this.params).then(response => {
        if (response.code === 200) {
          this.services = response.data.records
          this.page.current = response.data.current
          this.page.size = response.data.current
          this.page.total = response.data.total
          this.page.pages = response.data.pages
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
        this.loading = false
      })
    },
    // 搜索
    search() {
      this.fetchData()
    },
    // 翻页
    turnPage() {
      this.fetchData()
    },
    // 禁用
    handleSwitch(row) {
      const locked = row.locked === 1 ? 0 : 1
      this.temp = {
        id: row.id,
        locked: locked
      }
      addOrUpdate(this.temp).then(response => {
        if (response.code === 200) {
          this.fetchData()
          this.$notify({
            title: '成功',
            message: row.name + '服务' + (locked === 1 ? '禁用成功' : '开启成功'),
            type: (locked === 1 ? 'success' : 'warning'),
            duration: 2000
          })
        } else {
          this.$notify({
            title: '请求失败',
            message: response.message,
            type: 'error',
            duration: 2000
          })
        }
      })
      row.enabled = locked
    },
    // 打开添加
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogForm = true
      this.form.id = null
      this.form.name = null
      this.form.code = null
      this.form.description = null
      this.form.locked = 0
    },
    // 打开编辑
    handleEdit(index, row) {
      this.dialogStatus = 'update'
      this.dialogForm = true
      if (this.form.id === null || this.form.id === undefined || this.form.id !== row.id) {
        this.form.id = row.id
        this.form.name = row.name
        this.form.code = row.code
        this.form.description = row.description
        this.form.locked = row.locked
      }
    },
    // 添加或修改菜单
    handleAddOrUpdate() {
      this.buttonLoading = true
      addOrUpdate(this.form).then(response => {
        if (response.code === 200) {
          this.fetchData()
          this.$notify({
            title: '成功',
            message: '保存成功',
            type: 'success',
            duration: 2000
          })
        } else {
          this.$notify({
            title: '保存失败',
            message: response.message,
            type: 'error',
            duration: 2000
          })
        }
        this.buttonLoading = false
        this.dialogForm = false
      })
    },
    // 刷新
    refresh() {
      this.fetchData()
    },
    // 获取服务端允许的客户端
    handleAllowedClient(row) {
      getAllowedClient({ serviceId: row.id }).then(response => {
        if (response.code === 200) {
          this.allowedClients = response.data
          this.dialogAllowedClient = true
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
      })
    },
    // 获取客户端可以访问的服务端
    handleAuthorityServiceInfo(row) {
      getAuthorityServiceInfo({ clientId: row.id }).then(response => {
        if (response.code === 200) {
          this.authorityServiceInfos = response.data
          this.dialogAuthorityServiceInfo = true
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
      })
    }
  }
}
</script>

<style>

</style>
